package b3.widget.national
{
	import b3.config.*;
	import b3.model.*;
	import b3.net.*;
	import b3.util.*;
	import b3.widget.*;
	import b3.widget.basic.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class NationalDialog extends BasicWidget
	{
		public function NationalDialog()
		{
			super();
			this.anchor = ANCHOR_MIDDLE_MIDDLE;
			//背景
			var NationalDialogBgr:Class = ResourceHelper.getInstance().getClassByName("NationalDialogBgr");
			addChild(new NationalDialogBgr());
			//标题
			var titleText:TextField = new TextField();
			titleText.selectable = false;
        	titleText.autoSize = TextFieldAutoSize.CENTER;
			titleText.defaultTextFormat = new TextFormat("微软雅黑", 26, 0xFFFF00, true);
			titleText.filters = [new GlowFilter(0xFF0000, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			titleText.text = "国庆节收集五星活动";
			titleText.x = (this.width - titleText.width) / 2;
			titleText.y = 100;
			addChild(titleText);
			
			//免费领取
			var freeItem:FreeMonumentItem = new FreeMonumentItem(510);
			freeItem.x = 80;
			freeItem.y = 150;
			addChild(freeItem);
			
			//兑换区
			for (var i:int = 0; i < 4; i++)
			{
				var exchangeItem:ExchangeMonumentItem = new ExchangeMonumentItem(511 + i);
				exchangeItem.x = 80 + 190 * (i % 2);
				exchangeItem.y = 250  + 105 * int(i / 2);
				addChild(exchangeItem);
			}
			
			//我目前收集的五星
			//描述文字
			var descriptionText:TextField = new TextField();
			descriptionText.selectable = false;
        	descriptionText.autoSize = TextFieldAutoSize.LEFT;
			descriptionText.defaultTextFormat = new TextFormat("微软雅黑", 12, 0x000000);;
			descriptionText.text = "我目前收集的五星";
			descriptionText.x = 300;
			descriptionText.y = 180;
			addChild(descriptionText);
			//五星数目
			var starPane:NationalStarPane = new NationalStarPane();
			starPane.x = 310;
			starPane.y = 210;
			addChild(starPane);
			
			//提示信息
			var messageText:TextField = new TextField();
			messageText.selectable = false;
        	messageText.autoSize = TextFieldAutoSize.LEFT;
        	messageText.wordWrap = true;
        	messageText.width = 280;
			messageText.defaultTextFormat = new TextFormat("微软雅黑", 15, 0x000000);;
			messageText.text = "";
			messageText.x = 130;
			messageText.y = 165;
			addChild(messageText);
			
			//状态 
			var statusText:TextField = new TextField();
			statusText.selectable = false;
        	statusText.autoSize = TextFieldAutoSize.RIGHT;
			statusText.defaultTextFormat = new TextFormat("微软雅黑", 18, 0x0000FF);
			statusText.text = "";
			statusText.x = 390 - statusText.width;
			statusText.y = 110;
			addChild(statusText);
			
			//关闭按钮
			var CloseButton:Class = ResourceHelper.getInstance().getClassByName("CloseButtonNational");
			var buttonClose:BasicButton = new BasicButton(CloseButton);
			buttonClose.x = 220;
			buttonClose.y = 475;
			buttonClose.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			addChild(buttonClose);
			this.x = -this.width / 2;
			this.y = -this.height / 2;
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		private function onAddedToStage(event:Event):void
		{
			var me:Player = PlayerManager.getInstance().me;
			me.packageModel.addEventListener(ModelEvent.PURCHASE_COMPLETE, onPurchaseComplete, false, 0, true);
		}

		private function onRemovedFromStage(event:Event):void
		{
			var me:Player = PlayerManager.getInstance().me;
			me.packageModel.removeEventListener(ModelEvent.PURCHASE_COMPLETE, onPurchaseComplete);
		}
		
		
		private function onButtonOKClick(event:MouseEvent):void
		{
			close();
		}
        
		private function onPurchaseComplete(event:ModelEvent):void
		{
			var data:Object = event.data;
			var popMessage:PopMessage;
			if (data["err"] == 0)
			{
				var purchaseObjIndex:int = data['purchaseObjIndex'];
				popMessage = new PopMessage("兑换成功！请在包裹中查看！", 0x00AA00);
			}
			else
			{
				popMessage = new PopMessage("兑换失败！国庆五星不足！", 0xFF0000);
			}
			WidgetManager.getInstance().popOut([popMessage]);
		}
		
	}
}